package com.yq.ylmall.acl.helper;


import com.yq.ylmall.model.acl.Menu;

import java.util.ArrayList;
import java.util.List;

public class MenuHelper {
    /**
     * 使用递归方法建菜单
     * @param sysMenuList
     * @return
     */
    public static List<Menu> buildTree(List<Menu> sysMenuList) {
        List<Menu> trees = new ArrayList<>();
        for (Menu sysMenu : sysMenuList) {
            if (sysMenu.getParentId().longValue() == 0) {
                trees.add(findChildren(sysMenu,sysMenuList));
            }
        }
        return trees;
    }

    /**
     * 递归查找子节点
     * @param treeNodes
     * @return
     */
    public static Menu findChildren(Menu sysMenu, List<Menu> treeNodes) {
        sysMenu.setChildren(new ArrayList<Menu>());
        for (Menu it : treeNodes) {
            if(sysMenu.getId().longValue() == it.getParentId().longValue()) {
                sysMenu.getChildren().add(findChildren(it,treeNodes));
            }
        }
        return sysMenu;
    }
}
